package com.macro.mall.tiny.controller;

import com.macro.mall.tiny.common.api.CommonPage;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.mbg.model.PmsBrand;
import com.macro.mall.tiny.service.PmsBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/brand")
@Api(tags="PmsBrandController")
@Tag(name="PmsBrandController",description = "品牌管理相关接口")
@PreAuthorize("@ex.hasAuthority('/brand')")
public class PmsBrandController {

    @Autowired
    private PmsBrandService pmsBrandService;

    //获取全部品牌列表
    @GetMapping("/listAll")
    @ApiOperation("获取全部品牌列表")
//    @ResponseBody
    public CommonResult<List<PmsBrand>> getList(){
       log.info("获取全部品牌列表");

        List<PmsBrand> pmsBrandList=pmsBrandService.getList();
        return CommonResult.success(pmsBrandList);
    }

    //新增品牌
    @PostMapping("/create")
    @ApiOperation("新增品牌")
//    @ResponseBody
    public CommonResult add(@RequestBody PmsBrand pmsBrand){
        log.info("新增品牌：{}",pmsBrand);

        int code=pmsBrandService.add(pmsBrand);
        return code==1? CommonResult.success() : CommonResult.failed();
    }

    //更新品牌
    @PostMapping("/update/{id}")
    @ApiOperation("更新指定品牌")
//    @ResponseBody
    public CommonResult updateById(@PathVariable Long id,@RequestBody PmsBrand pmsBrand){
        log.info("更新指定品牌：{},{}",id,pmsBrand);

        int code=pmsBrandService.updateById(id,pmsBrand);
        return code==1? CommonResult.success():CommonResult.failed();
    }

    //删除品牌
    @GetMapping("/delete/{id}")
    @ApiOperation("删除品牌")
//    @ResponseBody
    public CommonResult deleteById(@PathVariable Long id){
        log.info("根据id删除品牌：{}",id);

        int code=pmsBrandService.deleteById(id);
        return code==1? CommonResult.success():CommonResult.failed();
    }

    //根据品牌名称分页获取品牌列表
    @GetMapping("/brand/list")
    @ApiOperation("根据品牌名称分页获取品牌列表")
//    @ResponseBody
    public CommonResult<CommonPage<PmsBrand>> getListPage(
            @RequestParam(value="keyword",required=false) String keyword,
            @RequestParam(value="showStatus",required = false) Integer showStatus,
            @RequestParam(value="pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(value="pageSize",defaultValue = "5")Integer pageSize){
        log.info("根据品牌名称分页获取品牌列表：{}{}{}{}",keyword,showStatus,pageNum,pageSize);

        List<PmsBrand> pmsBrandList=pmsBrandService.getListPage(keyword,showStatus,pageNum,pageSize);
        return CommonResult.success(CommonPage.restPage(pmsBrandList));

    }

    //根据编号获取品牌信息
    @GetMapping("/{id}")
    @ApiOperation("根据编号获取品牌信息")
//    @ResponseBody
    public CommonResult<PmsBrand> getById(@PathVariable Long id){
        log.info("根据编号获取品牌信息：{}",id);

        PmsBrand pmsBrand=pmsBrandService.getById(id);
        return CommonResult.success(pmsBrand);
    }

    //批量删除品牌
    @PostMapping("/delete/batch")
    @ApiOperation("批量删除品牌")
//    @ResponseBody
    public CommonResult deleteByBatch(@RequestParam(value="ids") List<Long> ids){
        log.info("批量删除品牌：{}",ids);

        Integer number=ids.size();
        Integer code=pmsBrandService.deleteByBatch(ids);
        if(code==number){
            return CommonResult.success();
        }else{
            return CommonResult.failed("部分或者全部删除未成功");
        }

    }

    //批量更新品牌显示状态
    @PostMapping("/update/showStatus")
    @ApiOperation("批量更新品牌显示状态")
//    @ResponseBody
    public CommonResult updateshowStatusByBatch(
            @RequestParam(value="ids") List<Long> ids,
            @RequestParam(value="showStatus") Integer showStatus){
        log.info("批量更新品牌显示状态：{}{}",ids,showStatus);

        Integer number=ids.size();
        Integer code=pmsBrandService.updateshowStatusByBatch(ids,showStatus);
        if(number==code){
            return CommonResult.success();
        }else{
            return CommonResult.failed("批量更新品牌显示状态未成功");
        }

    }


    //批量更新品牌制造商状态
    @PostMapping("/update/factoryStatus")
    @ApiOperation("批量更新品牌制造商状态")
//    @ResponseBody
    public CommonResult updateFactoryStatusByBatch(
            @RequestParam(value="factoryStatus") Integer factoryStatus,
            @RequestParam(value="ids") List<Long> ids){
        log.info("批量更新品牌制造商状态；{}{}",factoryStatus,ids);

        Integer number=ids.size();
        Integer code=pmsBrandService.updateFactoryStatusByBatch(factoryStatus,ids);
        if(number==code){
            return CommonResult.success();
        }else{
            return CommonResult.failed("部分或者全部更新未成功");
        }
    }


}
